﻿using System;
using System.Collections.Generic;
using System.Text;
using POSPlus.EZBarTender.DatabaseOperations;
using System.Data.SqlClient;

namespace POSPlus.DatabaseOperations
{
	public class ProductBatch : DatabaseObject
	{
		public String BatchNo { get; set; }
		public String Upc { get; set; }
		public Int16 SpecialPriceMethod { get; set; }
		public Decimal SpecialPrice { get; set; }
		public Decimal SpecialGroupPrice { get; set; }
		public Double SpecialQuantity { get; set; }
		public DateTime EndDate { get; set; }
		public DateTime DealEndDate { get; set; }

		public ProductBatch(String connectionString)
		{
			ConnectionString = connectionString;
			PopulateDefault();
		}

		public ProductBatch(String connectionString, String batchNo, String upc)
		{
			ConnectionString = connectionString;
			using (SqlConnection connection = new SqlConnection(ConnectionString))
			{
				connection.Open();
				using (SqlCommand command = new SqlCommand("SELECT BatchNo, Upc, SpecialPriceMethod, Special_Price, " +
					"SpecialGroupPrice, SpecialQuantity, End_Date, DealEndDate FROM ProductBatches WHERE BatchNo = @BatchNo AND Upc = @Upc", connection))
				{
					command.Parameters.AddWithValue("@BatchNo", batchNo);
					command.Parameters.AddWithValue("@Upc", upc);
					using (SqlDataReader reader = command.ExecuteReader())
					{
						if (reader.Read())
						{
							BatchNo = FromDB<String>(reader["BatchNo"]);
							Upc = FromDB<String>(reader["Upc"]);
							SpecialPriceMethod = FromDB<Int16>(reader["SpecialPriceMethod"]);
							SpecialPrice = FromDB<Decimal>(reader["Special_Price"]);
							SpecialGroupPrice = FromDB<Decimal>(reader["SpecialGroupPrice"]);
							SpecialQuantity = FromDB<Double>(reader["SpecialQuantity"]);
							EndDate = FromDB<DateTime>(reader["End_Date"]);
							DealEndDate = FromDB<DateTime>(reader["DealEndDate"]);
						}
						else
						{
							PopulateDefault();
						}
					}
				}
			}
		}

		protected override void PopulateDefault()
		{
			BatchNo = "";
			Upc = "";
			SpecialPriceMethod = -1;
			SpecialGroupPrice = -1;
			SpecialPrice = -1;
			SpecialQuantity = -1;
		}

		public override Boolean Create()
		{
			throw new NotImplementedException();
		}

		public override Boolean Update()
		{
			throw new NotImplementedException();
		}

		public override Boolean Delete()
		{
			throw new NotImplementedException();
		}

		public static List<ProductBatch> GetAllByUpc(string connectionString, string Upc)
		{
			List<ProductBatch> result = new List<ProductBatch>();
			using (SqlConnection connection = new SqlConnection(connectionString))
			{
				connection.Open();
				using (SqlCommand command = new SqlCommand("SELECT BatchNo, Upc, SpecialPriceMethod," +
					"SpecialGroupPrice,Special_Price,SpecialQuantity, End_Date, DealEndDate FROM ProductBatches WHERE " +
					"Upc = @Upc", connection))
				{
					command.Parameters.AddWithValue("@Upc", Upc);
					using (SqlDataReader reader = command.ExecuteReader())
					{
						while (reader.Read())
						{
							ProductBatch batch = new ProductBatch(connectionString);
							batch.BatchNo = FromDB<String>(reader["BatchNo"]);
							batch.SpecialGroupPrice = FromDB<Decimal>(reader["SpecialGroupPrice"]);
							batch.SpecialPrice = FromDB<Decimal>(reader["Special_Price"]);
							batch.SpecialPriceMethod = FromDB<Int16>(reader["SpecialPriceMethod"]);
							batch.SpecialQuantity = FromDB<Double>(reader["SpecialQuantity"]);
							batch.Upc = FromDB<String>(reader["Upc"]);
							batch.EndDate = FromDB<DateTime>(reader["End_Date"]);
							batch.DealEndDate = FromDB<DateTime>(reader["DealEndDate"]);
							result.Add(batch);
						}
					}
				}
			}
			return result;
		}
	}
}
